package com.siruier.boss.ad

import android.content.Context
import com.siruier.boss.ad.service.IAdSdkService
import com.siruier.boss.ad.service.impl.GdtSdkServiceImpl
import com.siruier.boss.ad.service.impl.PangleSdkServiceImpl

typealias AdVideoCompleted = (isFirst: Boolean) -> Unit

/**
 * 广告服务sdk统一入口服务类
 */
object AdSdk : IAdSdkService {
    /**
     * 全屏视频类型
     */
    const val NATIVE_VIDEO: String = "VIDEO"

    private val SDK_SERVICE: MutableList<IAdSdkService> = mutableListOf()
    private val SDK_PLATFORM: Map<String, IAdSdkService>

    var adVideoCompleted: AdVideoCompleted? = null

    private var isInit = false

    init {
        SDK_SERVICE.add(PangleSdkServiceImpl())
        SDK_SERVICE.add(GdtSdkServiceImpl())
        SDK_PLATFORM = SDK_SERVICE.associateBy { it.adPlatform }
    }

    override suspend fun sdkInit(context: Context): Boolean {
        if (!isInit) {
            isInit = SDK_SERVICE.map { it.sdkInit(context) }.all { it }
        }
        return isInit
    }

    override fun requestPermissionIfNecessary(context: Context) {
        SDK_SERVICE.forEach { it.requestPermissionIfNecessary(context) }
    }

    override suspend fun loadExpressDrawNativeAd(context: Context, width: Int, height: Int): List<AdBean> {
//        return SDK_PLATFORM["PANGLE"]!!.loadExpressDrawNativeAd(context, width, height)
//        return SDK_PLATFORM["GDT"]!!.loadExpressDrawNativeAd(context, width, height)
        return SDK_SERVICE.map { it.loadExpressDrawNativeAd(context, width, height) }.flatten()
    }

    override fun adDestroy(ad: AdBean) {
        SDK_PLATFORM[ad.adPlatform]?.adDestroy(ad)
        VideoPayTimeTimer.removeTimer(ad.unKey)
    }
}